package mobidever.godutch.database.base;

import java.util.ArrayList;

import mobidever.godutch.utility.Reflection;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SQLiteHelper extends SQLiteOpenHelper {
	private static SQLiteDateBaseConfig SQLITE_DATEBASE_CONFIG;
	private Context mContext;
	private static SQLiteHelper INSTANCE;
	private Reflection mReflection ;
	
	public interface SQLiteDataTable{
		void OnCreate(SQLiteDatabase pDataBase);
		void OnUpgrade(SQLiteDatabase pDataBase);
	}
	private SQLiteHelper(Context pContext){
		super(pContext,SQLITE_DATEBASE_CONFIG.getDataBaseName(),null,SQLITE_DATEBASE_CONFIG.getVersion());
		mContext = pContext;
	}
	public static SQLiteHelper getInstance(Context pContext){
		if(INSTANCE == null){
			SQLITE_DATEBASE_CONFIG = SQLiteDateBaseConfig.getInstance(pContext);
			INSTANCE = new SQLiteHelper(pContext);
		}
		return INSTANCE;
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		ArrayList<String> _ArrayList = SQLITE_DATEBASE_CONFIG.getTables();
		mReflection = new Reflection();
		for(int i=0;i<_ArrayList.size();i++){
			try {
				SQLiteDataTable _SQLiteDataTable = (SQLiteDataTable) mReflection.newInstance(_ArrayList.get(i), new Object[] {mContext}, new Class[]{Context.class});
				_SQLiteDataTable.OnCreate(db);
			} catch(Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

}
